<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      html,
      body {
        margin: 0;
        padding: 0;
      }

      body {
        height: 2000px;
        display: flex;
        flex-direction: column;
      }
      .top {
        height: 200px;
        text-align: center;
        overflow: hidden;
      }
      button {
        padding: 10px 20px;
        font-size: 30px;
        cursor: pointer;
      }
      .content {
        flex: 1;
        background-color: orange;
        display: flex;
      }
      .content .left {
        flex: 1;
        background-color: skyblue;
        padding: 10px;
        box-sizing: border-box;
      }
      .content .item {
        display: inline-block;
        height: 30px;
        border: 1px solid #000;
        padding: 4px;
        line-height: 30px;
        border-radius: 10px;
        transition: 0.3s;
        cursor: pointer;
        margin-right: 10px;
      }
      .content .item:hover {
        box-shadow: 0 0 10px gray;
        transform: translateY(-4px);
        color: white;
        font-weight: 900;
      }

      .content .right {
        flex: 1;
        background-color: pink;
      }
    </style>
  </head>
  <body>
    <div class="top">
      <h2>抽奖</h2>
      <button>开始</button>
    </div>
    <div class="content">
      <div class="left">
        <div class="item">
          某同学-0次
        </div>
      </div>
      <div class="right"></div>
    </div>
    <script src="./lib/echarts.min.js"></script>
    <script src="./node_modules/axios/dist/axios.js"></script>
    <script>
     
      // 生成图形
     
      (async function getName(){
        const left = document.querySelector('.left')
        const url_students = 'http://127.0.0.1:7000/api/students'
        const res = await axios(url_students)
        console.log(res)
        const left_student = res.data.map(el=>{
          const {name,count,id} =el
          return ` <div class="item" data-id =${id}>
          ${name}-${count}次
        </div>
          `

        })

        left.innerHTML = left_student.join('')

      })()
      //渲染右边
      const url_lucy = 'http://127.0.0.1:7000/api/lucystar'
      const url_students = 'http://127.0.0.1:7000/api/students'
      function students(arr){
        var myChart = echarts.init(document.querySelector('.right'))
        
    
        const option = {
          xAxis: {
            type: 'value'
          },
          yAxis: {
            type: 'category',
            data:arr.map(el =>el.name),
            inverse: true
          },
          tooltip: {},
          series: [
            {
              realtimeSort: true,
              type: 'bar',
               data:arr === Array? arr.map(el =>el.count):arr.count,
              showBackground: true,
              backgroundStyle: {
                color: 'rgba(180, 180, 180, 0.2)'
              }
            }
          ]
        }
        myChart.setOption(option)
      
    }
       
      window.addEventListener('load',async function(){
      const {data:res} = await axios.get(url_students)
       console.log(res)
       students(res)
       
     })

      
    
      //点击事件,点击随机获取一个学生
      const one = document.querySelector('.top button')
      one.addEventListener('click',async (e)=>{
        try{
         const {data:res} = await axios.get(url_lucy)
        const items = document.querySelectorAll('.item')
        alert (`恭喜${res.name}被抽中`)
        const id = res.id
        console.log(id)
        items[id].innerHTML =`${res.name}-${res.count}次`
        res.count++
        console.log(res)
        localStorage.setItem('arr3',JSON.stringify(res))

        } catch(err){
          console.log(err)
        }
       

      })
     

     

    </script>
  </body>
</html>
